import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import * as global from "@/common/js/global"
import { getItem, setItem } from '@/utils/cookieUtil'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}


/**
 * 静态路由
 */
export const constantRouterMap = [

	// 重定向，每次访问*都定向到首页
	{
		path: '',
		redirect: '/disk/home',
		hidden: true,
	},
	{
		path: '/disk/home',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/home'),
	},
	{
		path: '/disk/share',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/share'),
	},
	{
		path: '/login',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/login'),
	},
	{
		path: '/disk/recycle',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/recycle'),
	},
	{
		path: '/disk/public',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/public'),
	},
	{
		path: '/check/share/:uuid',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/check'),
	},
	{
		path: '/file/share/:uuid',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/user-share'),
	},
	{
		path: '/user',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		redirect: '/user/info',
		component: () => import('@/pages/user-center'),
		children: [
			{
				path: 'info',
				meta: {
					icon: null,
					title: null,
				},
				hidden: true,
				sort: null,
				component: () => import('@/pages/user-center/info'),
			},
			{
				path: 'security',
				meta: {
					icon: null,
					title: null,
				},
				hidden: true,
				sort: null,
				component: () => import('@/pages/user-center/security'),
			},
			{
				path: 'analysis',
				meta: {
					icon: null,
					title: null,
				},
				hidden: true,
				sort: null,
				component: () => import('@/pages/user-center/analysis'),
			}
		]
	},
	{
		path: '/comment',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/comment'),
	},
	{
		path: '/disk/im',
		meta: {
			icon: null,
			title: null,
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/im'),
		children: [
			{
				path: 'message',
				meta: {
					icon: null,
					title: null,
				},
				hidden: true,
				sort: null,
				component: () => import('@/pages/im/message'),
			},
			{
				path: 'contacts',
				meta: {
					icon: null,
					title: null,
				},
				hidden: true,
				sort: null,
				component: () => import('@/pages/im/contacts'),
			},
		]
	},
	{
		path: '/404',
		meta: {
			icon: null,
			title: "404",
		},
		hidden: true,
		sort: null,
		component: () => import('@/pages/error'),
	},

	{
		path: '*',
		redirect: '/404',
		hidden: true
	}
]
/**
 * 动态路由
 */
export const asyncRouterMap = [



]


const router = new VueRouter({
	mode: "history",
	routes: constantRouterMap,
	// // 控制路由跳转滚动条的位置为最顶部
	scrollBehavior(to, from, savedPosition) {
		// 始终滚动到顶部，y代表距离顶部的距离
		return { y: 0 }
	},
})

const whiteList = ['/login', '/file/share/'] // 不重定向白名单

router.beforeEach((to, from, next) => {
	if (getItem("token")) {
		if (to.path === '/login') {
			next({ path: '/disk/home?category=0' })
		} else {
			if (store.state.user.roleList.length == 0) {
				// 拉取用户信息
				getUserInfo()
			}
			next()
		}
	} else {
		if (to.path == '/login' || to.path.match('/file/share/')) {
			next()
		} else {
			next("/login")
		}
	}
	next()
})

router.afterEach(() => {

});

async function getUserInfo(next) {
	let res = await store.dispatch('getUserInfo')
	if (!res.success) global.resError(res)
	else getSpaceInfo(next)
}

async function getSpaceInfo(next) {
	let res = await store.dispatch('getSpace')
	if (!res.success) global.resError(res)
	else next
}

export default router